package controllers;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import models.BillItem;
import models.Category;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static play.libs.Json.toJson;

/**
 * Created by duzi on 7/7/15.
 */
public class report extends Controller {

    public Result yearChart(){
        String startDate;
        String endDate;
        String month;
        List<String> f = new ArrayList();

        for(int i =1; i < 13; i++){
            if(i < 10){
                month = "0" + i;
            }else{
                month = i + "";
            }

            startDate = "2015-" + month  + "-01" + " 00:00:00";
            endDate = "2015-" + month + "-30" + " 23:59:59";

            List<BillItem> billItems = BillItem.find.where()
                    .eq("bill_item_status", 1)
                    .ge("bill_item_createtime", transformDate(startDate))
                    .le("bill_item_createtime", transformDate(endDate))
                    .findList();

            List<BillItem> resultItems = new ArrayList<>();
            float sumPrice = 0.0f;
            for(BillItem item : billItems){
                sumPrice += Double.parseDouble(item.billItemSumPrice);
            }
            f.add(sumPrice + "");
        }
        return ok(toJson(f.toString()));
    }

    public Result categoryChart(){
        JsonNode timeJson = request().body().asJson();
        String startDate = timeJson.get("start").asText() + " 00:00:00";
        String endDate = timeJson.get("end").asText() + " 23:59:59";

        List<Category> category = Category.find.select("categoryName").findList();

        ObjectNode res = Json.newObject();
        for(Category c : category){
            res.put(c.categoryName, 0);
        }

        res.put("all", 0);

        List<BillItem> billItems = BillItem.find.where()
                .eq("bill_item_status", 1)
                .ge("bill_item_createtime", transformDate(startDate))
                .le("bill_item_createtime", transformDate(endDate))
                .findList();

        for(BillItem bi : billItems){
            for(Category c : category){
                if(bi.product.category.categoryId == c.categoryId ){
                    Double price = res.get(c.categoryName).asDouble() +  Double.parseDouble(bi.billItemSumPrice);
                    res.set(c.categoryName, toJson(price.toString()));
                }
            }
            Double sumPrice = res.get("all").asDouble() +  Double.parseDouble(bi.billItemSumPrice);
            res.set("all", toJson(sumPrice.toString()));
        }
        return ok(res);
    }
    public Date transformDate(String a){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date d;

        try{
            d = sdf.parse(a);
        }catch (java.text.ParseException ex){
            System.out.println("wrong date format");
            return null;
        }

        return d;
    }
}
